package liy.demo.wikiproject.controller;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import liy.demo.wikiproject.req.DocQueryReq;
import liy.demo.wikiproject.req.DocSaveReq;
import liy.demo.wikiproject.resp.DocResp;
import liy.demo.wikiproject.resp.CommonResp;
import liy.demo.wikiproject.resp.PageResp;
import liy.demo.wikiproject.service.DocService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/doc")
public class DocController {


    @Resource
    private DocService docService;


    @RequestMapping("all")
    public CommonResp<List<DocResp>> all(DocQueryReq req) {
        CommonResp<List<DocResp>> resp = new CommonResp<>();
        List<DocResp> all = docService.all(req);
        resp.setContent(all);
        return resp;
    }
    @RequestMapping("list")
    public CommonResp<PageResp<DocResp>> list( @Valid DocQueryReq req) {
        CommonResp<PageResp<DocResp>> resp = new CommonResp<>();
        PageResp<DocResp> list = docService.list(req);
        resp.setContent(list);
        return resp;
    }
    @RequestMapping(value = "save",method = {RequestMethod.PUT,RequestMethod.POST})
    public CommonResp save(@RequestBody DocSaveReq req) {
        CommonResp<DocSaveReq>resp = new CommonResp<>();
        docService.save(req);
        resp.setSuccess(true);
        return resp;
    }
    @RequestMapping("document/content")
    public CommonResp<DocResp> findDocument(DocQueryReq req) {
        CommonResp<DocResp> resp = new CommonResp<>();
        DocResp conent = docService.findConent(req);
        resp.setContent(conent);
        return resp;
    }
    @RequestMapping("document/vote")
    public CommonResp<String> addVote(DocQueryReq req) {
        CommonResp<String> resp = new CommonResp<>();
        docService.addVote(req);
        resp.setSuccess(true);
        resp.setContent("加了");
        return resp;
    }
    @RequestMapping("document/view")
    public CommonResp<String> addView(DocQueryReq req) {
        CommonResp<String> resp = new CommonResp<>();
        docService.addVote(req);
        resp.setSuccess(true);
        resp.setContent("加了");
        return resp;
    }
    @RequestMapping("delete/{id}")
    public CommonResp<String> deleteById(DocQueryReq req) {
        CommonResp<String> delete = docService.delete(req);
        return delete;
    }

}
